package com.zxn.ssyx.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxn.ssyx.common.result.Result;
import com.zxn.ssyx.model.acl.Admin;
import com.zxn.ssyx.model.acl.Role;
import com.zxn.ssyx.service.AdminService;
import com.zxn.ssyx.vo.acl.AdminQueryVo;
import com.zxn.ssyx.vo.acl.RoleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/acl/user")
@Api(tags = "用户管理")
@CrossOrigin  // 跨域
public class AdminController {
    @Autowired
    private AdminService adminService;

    @ApiOperation(value = "获取管理用户分页列表")
    @GetMapping("/{page}/{limit}")
    public Result index(@ApiParam(name = "page",value = "当前页码",required = true) @PathVariable Long page,
                        @ApiParam(name = "limit",value = "每页记录数",required = true) @PathVariable Long limit,
                        @ApiParam(name = "roleQueryVo",value = "查询对象",required = true) AdminQueryVo adminQueryVo){
        Page<Admin> pageParam = new Page<>(page,limit);
        IPage<Admin> rolePage = adminService.index(pageParam,adminQueryVo);
        return Result.ok(rolePage);
    }

    @ApiOperation(value = "根据id查询用户")
    @GetMapping("/get/{id}")
    public Result get(@ApiParam(name = "id",value = "用户id") @PathVariable Long id){
        Admin adminById = adminService.getById(id);
        return Result.ok(adminById);
    }

    @ApiOperation(value = "新增用户")
    @PostMapping("/save")
    public Result save(@ApiParam(name = "role",value = "新增用户信息") @RequestBody Admin admin){
        boolean save = adminService.save(admin);
        if (save){
            return Result.ok("保存用户成功！！");
        }
        return Result.fail("保存用户失败！！");
    }

    @ApiOperation(value = "修改用户")
    @PutMapping("/update")
    public Result updateById(@RequestBody Admin admin) {
        boolean result = adminService.updateById(admin);
        if (result){
            return Result.ok("修改用户成功！！");
        }
        return Result.fail("修改用户失败！！");
    }

    @ApiOperation(value = "删除用户")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id) {
        boolean result = adminService.removeById(id);
        if (result){
            return Result.ok("删除用户成功！！");
        }
        return Result.fail("删除用户失败！！");
    }

    @ApiOperation(value = "根据id列表删除用户")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        boolean result = adminService.removeByIds(idList);
        if (result){
            return Result.ok("根据id删除用户成功！！");
        }
        return Result.fail("根据id删除用户失败！！");
    }

}
